package com.zxy.express.exception;

import com.autopai.base.common.web.exception.BusinessException;
import com.autopai.base.common.web.model.RestResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常处理器
 *
 * @author yangyong
 * @since 1.0.0
 */
@RestControllerAdvice
public class BusinessExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(BusinessExceptionHandler.class);

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(BusinessException.class)
    public RestResult handleRRException(BusinessException ex) {
        RestResult RestResult = new RestResult();
        RestResult.error(ex.getCode(), ex.getMsg());

        return RestResult;
    }

    @ExceptionHandler(Exception.class)
    public RestResult handleException(Exception ex) {
        logger.error(ex.getMessage(), ex);
        return new RestResult().error();
    }
}
